//
//  NRWCooperateCityPickView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/20.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWCooperateCityPickView.h"

#import "NRWCooperationCityListModel.h"

@interface NRWCooperateCityPickView()<UIPickerViewDelegate, UIPickerViewDataSource>

@end

@implementation NRWCooperateCityPickView
{
    NSInteger _provinceIndex;
    NSInteger _cityIndex;
}

- (void)setSelectProvinceIndex:(NSInteger)selectProvinceIndex {
	
	_selectProvinceIndex = selectProvinceIndex;
	
	if (selectProvinceIndex == -1) {
		_provinceIndex = -1;
	}
	
	[self.pickerView selectRow:selectProvinceIndex inComponent:0 animated:NO];
	
	[self.pickerView reloadAllComponents];
}

- (void)setSelectCityIndex:(NSInteger)selectCityIndex {
	
	_selectCityIndex = selectCityIndex;
	
	if (selectCityIndex == -1) {
		selectCityIndex = -1;
	}
	
	[self.pickerView selectRow:selectCityIndex inComponent:1 animated:NO];
}

- (void)setCityModel:(NRWCooperationCityListModel *)cityModel {
    
    _cityModel = cityModel;
    
    [self.pickerView reloadAllComponents];
}

- (void)initPickView {
    
    [super initPickView];
    
    _provinceIndex = -1;
    
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerViewHeight = 300;
}

- (void)clickConfirmButton {
    
    if ([self.delegate respondsToSelector:@selector(cooperateCityPickView:confirmButtonDidClick:withProvinceName:provinceId:cityName:cityId:provinceIndex:cityIndex:)]) {
        
        if (_provinceIndex == -1) {
            [self.delegate cooperateCityPickView:self confirmButtonDidClick:self.confirmButton withProvinceName:@"" provinceId:@"" cityName:@"" cityId:@"" provinceIndex:0 cityIndex:0];
            return;
        }
        
        [self.delegate cooperateCityPickView:self
                       confirmButtonDidClick:self.confirmButton
                            withProvinceName:self.cityModel.result[_provinceIndex].provinceName
                                  provinceId:self.cityModel.result[_provinceIndex].provinceId
                                    cityName:self.cityModel.result[_provinceIndex].citys[_cityIndex].cityName
                                      cityId:self.cityModel.result[_provinceIndex].citys[_cityIndex].cityId
							   provinceIndex:_provinceIndex + 1
								   cityIndex:_cityIndex];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.cityModel.result.count + 1;
    } else {
        if ([pickerView selectedRowInComponent:0] == 0) {
            return 0;
        } else {
            return self.cityModel.result[[pickerView selectedRowInComponent:0] - 1].citys.count;
        }
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    NSString *text;
    if (component == 0) {
        if (row == 0) {
            text = @"全部";
        } else {
            text =  self.cityModel.result[row - 1].provinceName;
        }
    } else if (component == 1) {
        if ([pickerView selectedRowInComponent:0] == 0 || self.cityModel.result[[pickerView selectedRowInComponent:0] - 1].citys.count > row) {
            if ([pickerView selectedRowInComponent:0] == 0) {
                text = @"";
            } else {
                text =  self.cityModel.result[[pickerView selectedRowInComponent:0] - 1].citys[row].cityName;
            }
        }
    }
    
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:16];
    label.text = text;
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {

        [pickerView reloadComponent:1];
        
        if (row == 0) {
            _provinceIndex = _cityIndex = -1;
        } else {
            _provinceIndex = row - 1;
            _cityIndex = 0;
        }
    } else {
        _cityIndex = row;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 36;
}

@end
